"""
配置查看命令
"""
import os
import click
from config import Config
from i18n import t, set_language as set_runtime_language, get_supported_languages


LANGUAGE_CHOICES = [lang for lang in get_supported_languages()]


@click.command()
@click.option(
    "--language",
    "-l",
    type=click.Choice(LANGUAGE_CHOICES, case_sensitive=False),
    help="设置界面语言 / Set CLI language",
)
def config(language):
    """显示当前配置"""
    cfg = Config()

    if language:
        lang = language.lower()
        cfg.set_language(lang)
        set_runtime_language(lang)
        click.echo(t("config.language.updated", language=t(f"language.label.{lang}")))

    click.echo(t("config.header"))
    click.echo(t("config.global_config_path", path=cfg.global_config_path))
    click.echo(t("config.project_config_path", path=cfg.project_config_path))
    click.echo(t("config.project_root", root=cfg.workspace))
    click.echo(t("config.base_org", org=cfg.get_base_org()))
    click.echo(t("config.base_url", url=cfg.get_base_url()))
    click.echo(t("config.default_branch", branch=cfg.get_default_branch()))

    branch = cfg.get_branch()
    click.echo(t("config.current_branch", branch=branch))
    base_branch = cfg.get_base_branch()
    click.echo(t("config.base_branch", branch=base_branch))

    username = cfg.get_gitcode_username() or t("config.not_set")
    click.echo(t("config.gitcode_username", username=username))

    language_label = t(f"language.label.{cfg.get_language()}")
    click.echo(t("config.language.current", language=language_label))

    token = os.getenv("gitcode_password") or os.getenv("gitcode_token")
    if token:
        masked = token[:4] + "*" * (len(token) - 8) + token[-4:] if len(token) > 8 else "*" * len(token)
        click.echo(t("config.gitcode_token_set", token=masked))
    else:
        click.echo(t("config.gitcode_token_not_set"))
        click.echo(t("config.gitcode_token_instructions"))
        if os.name == "nt":
            click.echo(t("config.gitcode_token_windows_setx"))
            click.echo(t("config.gitcode_token_windows_session"))
        else:
            click.echo(t("config.gitcode_token_unix_temp"))
            click.echo(t("config.gitcode_token_unix_perm"))

    repos = cfg.get_repos()
    click.echo(t("config.repos_header", count=len(repos)))
    for repo_name in repos:
        click.echo(t("config.repo_item", repo=repo_name))

